<?php


function randNumber()
{
    return date('YmdHis', time()) . rand(0, 99) . rand(0, 99);
}

/**
 * 产生一个指定长度的随机字符串,并返回给用户
 * @param type $len 产生字符串的长度
 * @return string 随机字符串
 */
function genRandomString($len = 32)
{
    $chars = array(
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
        "3", "4", "5", "6", "7", "8", "9"
    );
    $charsLen = count($chars) - 1;
// 将数组打乱
    shuffle($chars);
    $output = "";
    for ($i = 0; $i < $len; $i++) {
        $output .= $chars[mt_rand(0, $charsLen)];
    }
    return $output;
}

/**
 * 获取配置信息
 * @param $value
 * @return mixed
 */

function configuration($value)
{
    return db('configuration')->value($value);
}

function videoImage($image)
{
    if (!$image) {
        return '';
    }
    return $image . '?spm=qipa250&x-oss-process=video/snapshot,t_0,w_200,m_fast';
}

function geturl($url)
{
    $headerArray = array("Content-type:application/json;", "Accept:application/json");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
//post curl
function RYpost($url, $data) {
    $header['App-Key'] = '3argexb63q4oe';
    $appSecret  = '31CcpFNPhE2Aa';
    $header['Nonce'] = rand();
    $header['Timestamp'] = time()*1000;
    $header['Signature'] = sha1($appSecret.$header['Nonce'].$header['Timestamp']);
    $headerArray = Array("App-Key:3argexb63q4oe","Nonce:".$header['Nonce'],"Timestamp:".$header['Timestamp']
    ,"Signature:".$header['Signature'],"Content-Type: application/x-www-form-urlencoded");
    //初使化init方法
    $ch = curl_init();
    //指定URL
    curl_setopt($ch, CURLOPT_URL, $url);
    //设定请求后返回结果
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //声明使用POST方式来进行发送
    curl_setopt($ch, CURLOPT_POST, 1);
    //发送什么数据呢
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //忽略证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    //忽略header头信息
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //设置超时时间
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
    //发送请求
    $output = curl_exec($ch);
    //关闭curl
    curl_close($ch);
    //返回数据
    return $output;
}

function getRyUserInfo($userId){
    $url = 'https://api-cn.ronghub.com/user/info.json?userId='.$userId;
    $data['userId'] = $userId;
    $ry = RYpost($url,$data);
    return json_decode($ry, true);
}

//关注状态
function attention($user_id, $attention_id){
    $user_attention = model('attention')->where(['user_id' => $user_id, 'attention_id' => $attention_id])->find() ? 1 : 0;
    $attention_user = model('attention')->where(['attention_id' => $user_id, 'user_id' => $attention_id])->find() ? 1 : 0;
    $attention = 0;
    if($user_attention == 1 && $attention_user == 1){
        $attention = 2;
    }else if($user_attention == 1){
        $attention = 1;
    }
    return $attention;
}
//用户年龄
function user_age($birthday){
    $date = date('Y', time());
    return (int)$date - (int)substr($birthday, 0, 4);//年龄
}
//评论列表
function commentList($dynamic_id){
    //评论列表
    $comment_list = model('dynamic_comment')
        ->alias('a')
        ->join('user_info b', 'a.user_id = b.user_id')
        ->where(['dynamic_id' => $dynamic_id])
        ->order('id desc')
        ->field("b.username,b.avatar,FROM_UNIXTIME(a.createtime,'%Y-%m-%d %H:%i:%s') as createtime,a.content,a.id,a.user_id,a.reply_id")
        ->select();
    foreach ($comment_list as $key=>$val){
        if($val['reply_id']){
            $comment_list[$key]['reply_name'] = model('user_info')->where('user_id', $val['reply_id'])->value('username');
        }
    }
    return $comment_list;
}